// EnumKarakter.cpp : Implementation of CEnumKarakter
#include "stdafx.h"
#include "EBASdll.h"
#include "EnumKarakter.h"
#include "Karakter.h"
#include "KarakterSkala.h"

/////////////////////////////////////////////////////////////////////////////
// CEnumKarakter

STDMETHODIMP CEnumKarakter::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IEnumKarakter
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CEnumKarakter::Next(/*[out]*/ IUnknown ** ppUnk, /*[out]*/ ULONG * pcFetched)
{
	_CommandPtr cmd;
	
	const char sql[255] = "EBAS_SP_HENT_KARAKTER";

	try {
		if (!ppUnk)
			return E_POINTER;

		ULONG cFetched = 0;

		if (m_first) {
			m_first = FALSE;

			cmd.CreateInstance(__uuidof(Command));
			cmd->ActiveConnection = CDatabase::GetConnection();
			cmd->CommandText = sql;
			cmd->CommandType = adCmdStoredProc;
			cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

			m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);	
			if (m_records->ADO_EOF)
				return E_ACCESSDENIED;
		}			

		if (m_records->ADO_EOF)
			return S_OK;

		m_length = (long)m_records->Fields->GetItem("ANTAL")->Value;

		CComObject<CKarakter> * pInstance = NULL;
		CComObject<CKarakter>::CreateInstance(&pInstance);
		pInstance->put_GUID( (bstr_t)m_records->Fields->GetItem("GUID")->Value );
		pInstance->put_Karakter( (bstr_t)m_records->Fields->GetItem("KARAKTER")->Value );

		CComObject<CKarakterSkala> * pSkala = NULL;
		CComObject<CKarakterSkala>::CreateInstance(&pSkala);
		pSkala->put_GUID( (bstr_t)m_records->Fields->GetItem("KARAKTER_SKALA_GUID")->Value );
		pSkala->put_Navn( (bstr_t)m_records->Fields->GetItem("KARAKTER_SKALA")->Value );

		pInstance->put_KarakterSkala(pSkala);
		pInstance->QueryInterface(ppUnk);

		m_pos += 1;
		cFetched = m_length;

		if (pcFetched)
			*pcFetched = cFetched;

		m_records->MoveNext();

	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return TRUE;
}

STDMETHODIMP CEnumKarakter::Skip(ULONG cItems)
{
	try {
		if (m_records) {
			m_pos += cItems;
			m_records->Move(m_pos);
		}
	} catch (_com_error &e) {

		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumKarakter::Reset()
{
	_CommandPtr cmd;

	const char sql[255] = "EBAS_SP_HENT_KARAKTER";

	try {
		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

		m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);
		if (m_records->ADO_EOF)
			return E_ACCESSDENIED;	
	} catch (_com_error &e) {
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumKarakter::Clone(IEnumeration ** ppEnumeration)
{
	return E_NOTIMPL;
}

STDMETHODIMP CEnumKarakter::findKarakterKarakterSkala(IKarakterSkala * pSkala)
{
	_CommandPtr cmd;
	
	const char sql[255] = "EBAS_SP_HENT_KARAKTER";

	try {
		BSTR	bstrSkala = bstr_t("");
		bstr_t	skala = bstr_t("");
				
		if (NULL == pSkala)
			return E_INVALIDARG;


		pSkala->get_Navn(&bstrSkala);
		skala = bstrSkala;

		_variant_t vKarakter;
		vKarakter.vt = VT_NULL;

		m_first = FALSE;

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("karakter", adVarChar, adParamInput, 255, vKarakter) );
		cmd->Parameters->Append( cmd->CreateParameter("karakterSkala", adVarChar, adParamInput, 255, skala) );

		m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (m_records->ADO_EOF)
			return E_ACCESSDENIED;

	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumKarakter::findKarakter(BSTR navn, IKarakter **ppKarakter)
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;
	
	const char sql[255] = "EBAS_SP_HENT_KARAKTER";

	try {
		bstr_t bstrNavn = navn;
				
		m_first = FALSE;

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("karakter", adVarChar, adParamInput, 255, bstrNavn) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;

		CComObject<CKarakter> * pInstance = NULL;
		CComObject<CKarakter>::CreateInstance(&pInstance);
		pInstance->put_GUID( (bstr_t)recordset->Fields->GetItem("GUID")->Value );
		pInstance->put_Karakter( (bstr_t)recordset->Fields->GetItem("KARAKTER")->Value );

		CComObject<CKarakterSkala> * pSkala = NULL;
		CComObject<CKarakterSkala>::CreateInstance(&pSkala);
		pSkala->put_GUID( (bstr_t)recordset->Fields->GetItem("KARAKTER_SKALA_GUID")->Value );
		pSkala->put_Navn( (bstr_t)recordset->Fields->GetItem("KARAKTER_SKALA")->Value );
		pInstance->put_KarakterSkala(pSkala);

		pInstance->QueryInterface(ppKarakter);

		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}
